﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Security.Claims;
using System.Reflection;

namespace Common
{
    public static class CommonHelper
    {
        public static string ToBase64String(this byte[] bytes)
        {
            return Convert.ToBase64String(bytes);
        }
        public static string ToBase64String(this string source, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.UTF8;
            }
            return Convert.ToBase64String(encoding.GetBytes(source));
        }
        
        public static bool IsNullOrEmpty(this string source)
        {

            if (string.IsNullOrEmpty(source))
            {
                return true;
            }
            return false;
        }
      
        public static bool NotNullOrEmpty(this string source)
        {

            if (string.IsNullOrEmpty(source))
            {
                return false;
            }
            return true;
        }
        public static bool ToBool(this string source)
        {
            if (bool.TryParse(source, out bool b))
            {
                return b;
            }
            return b;
        }
        public static int ToInt(this string source)
        {
            if (int.TryParse(source, out int b))
            {
                return b;
            }
            return b;
        }
        public static T JsonToObject<T>(this string source)
        {
            return System.Text.Json.JsonSerializer.Deserialize<T>(source);
        }

        public static string ToJson(this object source)
        {
            return System.Text.Json.JsonSerializer.Serialize(source);
        }
        public static string Sha256(this string input)
        {
            if (String.IsNullOrEmpty(input)) return string.Empty;
            using (var sha = SHA256.Create())
            {
                var bytes = Encoding.UTF8.GetBytes(input);
                var hash = sha.ComputeHash(bytes);
                return Convert.ToBase64String(hash);
            }
        }
        public static string GetUserId(this IIdentity identity)
        {
            if (!(identity is ClaimsIdentity claimsIdentity))
            {
                return null;
            }
            return claimsIdentity.FindFirst(ClaimTypes.NameIdentifier)?.Value;
        }
        /// <summary>
        /// 从类型成员获取指定Attribute特性
        /// </summary>
        /// <typeparam name="T">Attribute特性类型</typeparam>
        /// <param name="memberInfo">类型类型成员</param>
        /// <param name="inherit">是否从继承中查找</param>
        /// <returns>存在返回第一个，不存在返回null</returns>
        public static T GetAttribute<T>(this MemberInfo memberInfo, bool inherit = true) where T : Attribute
        {
            var attributes = memberInfo.GetCustomAttributes(typeof(T), inherit);
            return attributes.FirstOrDefault() as T;
        }
        #region  集合
        /// <summary>
        /// 判断集合是否为null或空集合
        /// </summary>
        public static bool IsNullOrEmpty<T>(this ICollection<T> collection)
        {
            return collection == null || collection.Count == 0;
        }
        /// <summary>
        /// 如果不存在，添加项
        /// </summary>
        public static void AddIfNotExist<T>(this ICollection<T> collection, T value, Func<T, bool> existFunc = null)
        {

            bool exists = existFunc == null ? collection.Contains(value) : existFunc(value);
            if (!exists)
            {
                collection.Add(value);
            }
        }

        /// <summary>
        /// 如果不为空，添加项
        /// </summary>
        public static void AddIfNotNull<T>(this ICollection<T> collection, T value) where T : class
        {
            if (value != null)
            {
                collection.Add(value);
            }
        }

        #endregion 
    }
}
